package com.greateffect.littlebud.lib.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by ZengCS on 2017/10/11.
 * E-mail:zengcs@vip.qq.com
 * Add:成都市天府软件园E3
 */
public class RoundRectColorView extends View {
    private Paint mPaint;
    private int mColor = Color.WHITE;

    public RoundRectColorView(Context context) {
        this(context, null);
    }

    public RoundRectColorView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundRectColorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true); //消除锯齿
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mPaint.setColor(mColor);
        mPaint.setStyle(Paint.Style.FILL); //绘制实心

        RectF rectF = new RectF(0, 0, getWidth(), getHeight());
        canvas.drawRoundRect(rectF, 2, 2, mPaint);
        // 如果是白色的话，还要画一个边框
        if (mColor == Color.WHITE) {
            mPaint.setColor(Color.parseColor("#D8D8D8"));
            mPaint.setStyle(Paint.Style.STROKE); //绘制空心
            mPaint.setStrokeWidth(2);
            canvas.drawRoundRect(rectF, 2, 2, mPaint);
        }
    }

    public void setColor(int mColor) {
        this.mColor = mColor;
        invalidate();
    }
}
